﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using Wise.TimeWarrior.DTO;
using Wise.TimeWarrior.Forms.Interfaces;

namespace Wise.TimeWarrior.Forms
{
    /// <summary>
    /// <see cref="IStatisticsForm"/>
    /// </summary>
    public partial class StatistcsForm : Form, IStatisticsForm
    {

        /// <summary>
        /// <see cref="IStatisticsForm.DateChanged"/>
        /// </summary>
        public event DateFilteredEventHandler DateChanged;

        /// <summary>
        /// <see cref="IStatisticsForm.WorkingDayList"/>
        /// </summary>
        public List<WorkingDayDto> WorkingDayList { get; set; }

        public event EventHandler DoSnapshoot;


        /// <summary>
        /// .ctor
        /// </summary>
        public StatistcsForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// <see cref="Form.OnShown"/>
        /// </summary>
        protected override void OnShown(EventArgs e)
        {
            var fromDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            dateFrom.Value = fromDate;


            var toDate = fromDate.AddMonths(1);
            dateTo.Value = toDate;
            BindSeriesToChart();
            base.OnShown(e);
        }
        /// <summary>
        /// <see cref="Form.OnFormClosing"/>
        /// </summary>
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                Visible = false;
            }
            else
            {
                base.OnFormClosing(e);
            }
        }




        private void BindSeriesToChart()
        {
            chartStatisticsData.Series.Clear();

            Series series = new Series("Work Statistics");
            Series standardTime = new Series("Regular 8hrs");

            series.ChartType = SeriesChartType.Line;
            standardTime.ChartType = SeriesChartType.Line;

            foreach (WorkingDayDto workingDayDto in WorkingDayList)
            {
                series.Points.AddXY(workingDayDto.CreationDateTime, (workingDayDto.LastAction.EventDate - workingDayDto.FirstAction.EventDate).TotalMinutes);
                standardTime.Points.AddXY(workingDayDto.CreationDateTime, 480);
            }


            chartStatisticsData.Series.Add(series);
            chartStatisticsData.Series.Add(standardTime);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DateChanged(dateFrom.Value, dateTo.Value);

            BindSeriesToChart();
        }

        private void createSnap_Click(object sender, EventArgs e)
        {
            if (DoSnapshoot != null)
                DoSnapshoot(this,EventArgs.Empty);

            DateChanged(dateFrom.Value, dateTo.Value);

            BindSeriesToChart();
        }
    }
}
